package com.cbw.wx.filter;

import com.cbw.bean.User;
import com.cbw.util.UserUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter({"/index.html"})
public class UserFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request=(HttpServletRequest) servletRequest;
        HttpServletResponse response=(HttpServletResponse) servletResponse;
        //从session获取登录者信息
        User wxUser = UserUtil.getWxUser(request.getSession());
        //判断是否登录
        if (wxUser!=null){
            filterChain.doFilter(servletRequest,servletResponse);//放行
        }else {
            response.sendRedirect("login.html");//未登录:重定向到登录页面
        }
    }

    @Override
    public void destroy() {

    }
}
